package com.youzan.cloud.biz.integral;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
 * @author hlin
 */
@Component
public class ContextUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ContextUtils.applicationContext = applicationContext;
    }

    public static <T> T getBean(String beanName) {
        return (T) applicationContext.getBean(beanName);
    }


    public static <T> T getBean(Class<T> beanClz) {
        return applicationContext.getBean(beanClz);
    }

    public static <T> Map<String, T> getBeans(Class<T> beanClz) {
        return applicationContext.getBeansOfType(beanClz);
    }

    public static <T> T registerBean(Class<T> beanClz) {
        return applicationContext.getAutowireCapableBeanFactory().createBean(beanClz);
    }
}

